package com.artPreter.service.impl;


import com.artPreter.mapper.EventExhibitionContentSectionMapper;
import com.artPreter.mapper.EventExhibitionSectionTempMapper;
import com.artPreter.mapper.TemplateDetailMapper;
import com.artPreter.pojo.*;
import com.artPreter.pojo.EventExhibitionSectionTemp;
import com.artPreter.service.IEventExhibitionContentSectionService;
import com.artPreter.service.IEventExhibitionSectionTempService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 活动展览栏目表 服务实现类
 * </p>
 *
 * @author jarvis
 * @since 2025-01-07
 */
@Service
public class EventExhibitionSectionTempServiceImpl extends ServiceImpl<EventExhibitionSectionTempMapper, EventExhibitionSectionTemp> implements IEventExhibitionSectionTempService {

    @Autowired
    private TemplateDetailMapper templateDetailMapper;
    @Autowired
    private IEventExhibitionContentSectionService eventExhibitionContentSectionService;
    @Autowired
    private EventExhibitionContentSectionMapper eventExhibitionContentSectionMapper;

    @Override
    public List<EventExhibitionSectionTemp> getList(EventExhibitionSectionTemp eventExhibitionSectionTemp) {
        List<EventExhibitionSectionTemp> list = new ArrayList<>();
        LambdaQueryWrapper<EventExhibitionSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(eventExhibitionSectionTemp.getEventId()!=null){
            lambdaQueryWrapper.eq(EventExhibitionSectionTemp::getEventId, eventExhibitionSectionTemp.getEventId());
            lambdaQueryWrapper.orderByAsc(EventExhibitionSectionTemp::getSort);
            list = this.baseMapper.selectList(lambdaQueryWrapper);
        }
        return list;
    }

    @Override
    public void add(EventExhibitionSectionTemp eventExhibitionSectionTemp) {
        String uuid = UUID.randomUUID().toString();
        if(eventExhibitionSectionTemp.getTempId()!=null){
            LambdaQueryWrapper<TemplateDetail> templateDetailLambdaQueryWrapper = new LambdaQueryWrapper<>();
            templateDetailLambdaQueryWrapper.eq(TemplateDetail::getTempId, eventExhibitionSectionTemp.getTempId());
            List<TemplateDetail> templateDetailList = templateDetailMapper.selectList(templateDetailLambdaQueryWrapper);
            if(templateDetailList.size()>0){
                // 模板地址
                eventExhibitionSectionTemp.setTempAddr(templateDetailList.get(0).getTempAddr());
                // 内容类型
                eventExhibitionSectionTemp.setContentType(templateDetailList.get(0).getContentType());
                // 模板名称
                eventExhibitionSectionTemp.setTempName(templateDetailList.get(0).getName());

                LambdaQueryWrapper<EventExhibitionSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                if(eventExhibitionSectionTemp.getEventId()!=null){
                    lambdaQueryWrapper.eq(EventExhibitionSectionTemp::getEventId, eventExhibitionSectionTemp.getEventId());
                }
                List<EventExhibitionSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
                if(list.size()==0){
                    eventExhibitionSectionTemp.setIsDefault(1);
                }
            }
        }
        eventExhibitionSectionTemp.setSectionId(uuid);
        this.baseMapper.insert(eventExhibitionSectionTemp);
        // 添加内容板块
        eventExhibitionContentSectionService.addContent(eventExhibitionSectionTemp);
    }

    @Override
    public void delete(EventExhibitionSectionTemp eventExhibitionSectionTemp) {
        this.baseMapper.deleteById(eventExhibitionSectionTemp);
        //如果该栏目是默认栏目，将其他栏目设置为默认栏目
        if(eventExhibitionSectionTemp.getIsDefault().equals(1)){
            LambdaQueryWrapper<EventExhibitionSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if(eventExhibitionSectionTemp.getEventId()!=null){
                lambdaQueryWrapper.eq(EventExhibitionSectionTemp::getEventId, eventExhibitionSectionTemp.getEventId());
            }
            List<EventExhibitionSectionTemp> list = this.baseMapper.selectList(lambdaQueryWrapper);
            if(list.get(0).getIsDefault().equals(1)){
                this.baseMapper.updateById(list.get(0));
            }
        }
        // 删除内容板块
        if(eventExhibitionSectionTemp.getSectionId()!=null&&!eventExhibitionSectionTemp.getSectionId().equals("")){
            LambdaQueryWrapper<EventExhibitionContentSection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(EventExhibitionContentSection::getSectionId, eventExhibitionSectionTemp.getSectionId());
            eventExhibitionContentSectionMapper.delete(lambdaQueryWrapper);
        }
    }

    @Override
    public void saveBatchData(List<EventExhibitionSectionTemp> eventExhibitionSectionTempList) {
        for (int i = 0; i < eventExhibitionSectionTempList.size(); i++) {
            // 首页导航
            if(eventExhibitionSectionTempList.get(i).getEventId().equals("358a5bee-7600-4c88-803c-1c92e74b8dc7")){
                EventExhibitionContentSection eventExhibitionContentSection = new EventExhibitionContentSection();
                eventExhibitionContentSection.setSort(eventExhibitionSectionTempList.get(i).getSort());
                eventExhibitionContentSection.setName(eventExhibitionSectionTempList.get(i).getName());
                LambdaQueryWrapper<EventExhibitionContentSection> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(EventExhibitionContentSection::getSectionId, eventExhibitionSectionTempList.get(i).getSectionId());
                eventExhibitionContentSectionMapper.update(eventExhibitionContentSection,lambdaQueryWrapper);
            }
            LambdaQueryWrapper<EventExhibitionSectionTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(EventExhibitionSectionTemp::getSectionId, eventExhibitionSectionTempList.get(i).getSectionId());
            this.baseMapper.update( eventExhibitionSectionTempList.get(i),lambdaQueryWrapper);
        }
    }
}
